package com.ruoyi.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.channel.constant.ChannelConstant;
import com.ruoyi.channel.service.IChannelBaseInfoService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.ChannelBaseInfo;
import com.ruoyi.laiqianba.enums.ChargeTypeEnum;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.service.IChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ChannelServiceImpl implements IChannelService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IChannelBaseInfoService channelBaseInfoService;

    @Override
    public AjaxResult insertChannelBaseInfo(ChannelBaseInfo channelBaseInfo) {

        if(StrUtil.isEmpty(channelBaseInfo.getChannelCode())){
            String channelCode = RandomUtil.randomStringUpper(6);
            channelBaseInfo.setChannelCode(channelCode);
        }

        if (StrUtil.equals(String.valueOf(channelBaseInfo.getChargeType()), String.valueOf(ChargeTypeEnum.REG.getValue()))){
            // 注册计价，需要生成用户信息
            String loginName = RandomUtil.randomNumbers(9);
            String loginPwd = RandomUtil.randomString(6);
            channelBaseInfo.setAdminName(loginName);
            channelBaseInfo.setAdminPassword(loginPwd);

            SysUser sysUser = this.addChannelRegUserInfo(channelBaseInfo);
            channelBaseInfo.setAdminUserid(sysUser.getUserId());

            if (ObjectUtil.isEmpty(sysUser)){
                return AjaxResult.error("新增渠道注册用户信息失败！");
            }
        }else if (StrUtil.equals(String.valueOf(channelBaseInfo.getChargeType()), String.valueOf(ChargeTypeEnum.UNION.getValue()))){
            // 联登渠道 生成渠道密钥
            String appSecret = RandomUtil.randomStringUpper(32);
            channelBaseInfo.setAppSecret(appSecret);
        }

        int i = channelBaseInfoService.insertChannelBaseInfo(channelBaseInfo);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * @return
     */
    public SysUser addChannelRegUserInfo(ChannelBaseInfo channelBaseInfo){
        //创建用户
        SysUser sysUser = new SysUser();
        sysUser.setNickName(channelBaseInfo.getChannelName() + "|" + channelBaseInfo.getChannelCode());
        sysUser.setUserName(channelBaseInfo.getAdminName());
        //密码加密
        String passward = SecurityUtils.encryptPassword(channelBaseInfo.getAdminPassword());
        sysUser.setPassword(passward);

        sysUser.setDeptId(ChannelConstant.CHANNEL_REG_PARENT_DEPT_ID);
        sysUser.setRoleId(ChannelConstant.CHANNEL_REG_ROLE_ID);
        sysUser.setRoleIds(new Long[]{ChannelConstant.CHANNEL_REG_ROLE_ID});

        int i = userService.insertUser(sysUser);
        if (i > 0){
            return sysUser;
        }
        return null;
    }

}
